package com.yungam.imcs.controller.line_manage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.yungam.common.bean.equip.Equipment;
import com.yungam.common.bean.line.ProductLine;
import com.yungam.common.bean.process.ProductionProcess;
import com.yungam.common.bean.product.ProductionProduct;
import com.yungam.common.response.RetDTO;
import com.yungam.common.service.EquipmentService;
import com.yungam.common.service.ProductLineService;
import com.yungam.common.service.ProductionProcessService;
import com.yungam.common.service.ProductionProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author liuyicheng
 * @description: 产线管理
 */

@Api(tags = "产线管理页面接口")
@RestController
@RequestMapping("/line")
public class LineController {

    @Resource
    private ProductLineService productLineService;
    @Resource
    private ProductionProductService productionProductService;
    @Resource
    private ProductionProcessService productionProcessService;
    @Resource
    private EquipmentService equipmentService;

    @ApiOperation("获取当前产线模式")
    @GetMapping("/state")
    public RetDTO<?> getLineState() {
        return RetDTO.data(productLineService.list().get(0));
    }

    @ApiOperation("更新产线模式")
    @PostMapping("/state")
    public RetDTO<?> updateLineState(@ApiParam("产线实体") @RequestBody ProductLine line) {
        line.setLineId(1);
        productLineService.updateById(line);
        return RetDTO.success();
    }

    /* 并列获取当前工序, 当前设备 */
    @ApiOperation("并列模式获取当前运行工序及设备")
    @GetMapping("/curProcess")
    public RetDTO<?> getCurProcess() {
        List<ProductionProduct> list = productionProductService.list(new QueryWrapper<ProductionProduct>()
                .eq("state", 0).isNull("end_time").orderByAsc("production_id"));
        if (list.size() == 0) return RetDTO.data(null);

        ProductionProduct product = list.get(0);
        List<ProductionProcess> processes = productionProcessService.list(new QueryWrapper<ProductionProcess>()
                .eq("product_sn", product.getProductSn()).eq("state", 0).isNull("end_time").orderByAsc("quality_id"));

        if (processes.size() == 0) return RetDTO.data(null);

        ProductionProcess pp = processes.get(0);
        Map<String, String> ans = Maps.newHashMap();
        ans.put("productSn", pp.getProductSn());
        ans.put("processNo", pp.getProcessNo());
        ans.put("processName", pp.getProcessName());
        ans.put("equipNo", pp.getEquNo());
        Equipment equipment = equipmentService.getOne(new QueryWrapper<Equipment>().eq("equ_number", pp.getEquNo()));
        String equipName = equipment == null ? "" : equipment.getEquName();
        ans.put("equipName", equipName);
        return RetDTO.data(ans);
    }

}
